<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Spring Boot&amp;nbsp;&amp;nbsp;Spring Boot是由Pivotal团队提供的全新框架，其设计目的是用来简化新Spring应用的初始搭建以及开发过程。它使用“习惯优于配置”的理念可以让你的项目快速运行部署。使用Spring Boot可以不用或者只需要很少的Spring配置。 &amp;nbsp;&amp;nbsp;而Spring Boot核心的功能就是自动配置。它会根据在类路径中的jar、类自动">
<meta property="og:type" content="article">
<meta property="og:title" content="浅谈Spring Boot自动配置的运作原理">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2016/07/29/2016-07-29-Spring-boot-autoconfigure/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="Spring Boot&amp;nbsp;&amp;nbsp;Spring Boot是由Pivotal团队提供的全新框架，其设计目的是用来简化新Spring应用的初始搭建以及开发过程。它使用“习惯优于配置”的理念可以让你的项目快速运行部署。使用Spring Boot可以不用或者只需要很少的Spring配置。 &amp;nbsp;&amp;nbsp;而Spring Boot核心的功能就是自动配置。它会根据在类路径中的jar、类自动">
<meta property="og:locale">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b6vnzlfgj20co0kimyz.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6b772ib2sj20he0ft77h.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b772m676j20gq07qdhi.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6b7h23prdj213c0kh7g2.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6b7lru822j20ei05ggmu.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b7lryzw2j20ut0ntaok.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/mw690/63503acbjw1f6b8n61hytj20fb04dgmt.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/63503acbjw1f6b9s692hxj20p30geq7e.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/63503acbjw1f6b9q6z1y8j20wc0idwkd.jpg">
<meta property="article:published_time" content="2016-07-29T10:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="Spring Boot">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b6vnzlfgj20co0kimyz.jpg">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/07/29/2016-07-29-Spring-boot-autoconfigure/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>浅谈Spring Boot自动配置的运作原理 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring-Boot"><span class="nav-number">1.</span> <span class="nav-text">Spring Boot</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AE%E7%9A%84%E8%BF%90%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="nav-number">2.</span> <span class="nav-text">自动配置的运作原理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E6%9F%A5%E7%9C%8B%E5%BD%93%E5%89%8D%E9%A1%B9%E7%9B%AE%E5%B7%B2%E5%90%AF%E5%8A%A8%E5%92%8C%E6%9C%AA%E5%90%AF%E5%8A%A8%E7%9A%84%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AE"><span class="nav-number">2.1.</span> <span class="nav-text">如何查看当前项目已启动和未启动的自动配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#SpringBootApplication"><span class="nav-number">2.2.</span> <span class="nav-text">@SpringBootApplication</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#EnableAutoConfiguration"><span class="nav-number">2.3.</span> <span class="nav-text">@EnableAutoConfiguration</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Conditional"><span class="nav-number">2.4.</span> <span class="nav-text">Conditional</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E6%9E%90-ConditionalOnNotWebApplication"><span class="nav-number">3.</span> <span class="nav-text">分析@ConditionalOnNotWebApplication</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E6%9E%90%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AE%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.</span> <span class="nav-text">分析自动配置的实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#properties%E9%85%8D%E7%BD%AE%E7%B1%BB"><span class="nav-number">4.1.</span> <span class="nav-text">properties配置类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E9%85%8D%E7%BD%AEBean"><span class="nav-number">4.2.</span> <span class="nav-text">自动配置Bean</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#end"><span class="nav-number">5.</span> <span class="nav-text">end</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2016/07/29/2016-07-29-Spring-boot-autoconfigure/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          浅谈Spring Boot自动配置的运作原理
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2016-07-29 18:00:00" itemprop="dateCreated datePublished" datetime="2016-07-29T18:00:00+08:00">2016-07-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%90%8E%E7%AB%AF/Java/Spring-Boot/" itemprop="url" rel="index"><span itemprop="name">Spring Boot</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="Spring-Boot"><a href="#Spring-Boot" class="headerlink" title="Spring Boot"></a>Spring Boot</h3><p>&nbsp;&nbsp;Spring Boot是由Pivotal团队提供的全新框架，其设计目的是用来简化新Spring应用的初始搭建以及开发过程。它使用“习惯优于配置”的理念可以让你的项目快速运行部署。使用Spring Boot可以不用或者只需要很少的Spring配置。</p>
<p>&nbsp;&nbsp;而Spring Boot核心的功能就是自动配置。它会根据在类路径中的jar、类自动配置Bean,当我们需要配置的Bean没有被Spring Boot提供支持时,也可以自定义自动配置。</p>
<h3 id="自动配置的运作原理"><a href="#自动配置的运作原理" class="headerlink" title="自动配置的运作原理"></a>自动配置的运作原理</h3><p><strong>&nbsp;&nbsp;Spring Boot自动配置其实是基于Spring 4.x提供的条件配置(Conditional)实现的。</strong></p>
<p>&nbsp;&nbsp;有关自动配置的源码在spring-boot-autoconfigure-1.x.x.jar内,如下图:</p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b6vnzlfgj20co0kimyz.jpg"></p>
<h4 id="如何查看当前项目已启动和未启动的自动配置"><a href="#如何查看当前项目已启动和未启动的自动配置" class="headerlink" title="如何查看当前项目已启动和未启动的自动配置"></a>如何查看当前项目已启动和未启动的自动配置</h4><ol>
<li>在application.propertie中设置debug=true属性.</li>
<li>在运行jar时添加–debug指令.</li>
</ol>
<p>&nbsp;&nbsp;当使用以上两种任意一种方法后,启动项目会在控制台输出已启动和未启动的自动配置日志.</p>
<h4 id="SpringBootApplication"><a href="#SpringBootApplication" class="headerlink" title="@SpringBootApplication"></a>@SpringBootApplication</h4><p>&nbsp;&nbsp;生成Spring Boot项目时,会自动生成一个入口类.入口类使用了@SpringBootApplication注解,它是Spring Boot的核心注解,它是一个组合注解,核心功能由@EnableAutoConfiguration注解提供.</p>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6b772ib2sj20he0ft77h.jpg" alt="@SpringBootApplication"></p>
<h4 id="EnableAutoConfiguration"><a href="#EnableAutoConfiguration" class="headerlink" title="@EnableAutoConfiguration"></a>@EnableAutoConfiguration</h4><p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b772m676j20gq07qdhi.jpg" alt="@EnableAutoConfiguration"></p>
<ol>
<li>@Import注解提供导入配置的功能,它导入了EnableAutoConfigurationImportSelector.</li>
<li>EnableAutoConfigurationImportSelector使用函数SpringFactoriesLoader.loadFactoryNames扫描META-INF/spring.factories文件中声明的jar包.</li>
</ol>
<p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6b7h23prdj213c0kh7g2.jpg"></p>
<ol start="3">
<li>spring.factories文件在spring-boot-autoconfigure-1.x.x.jar中.</li>
</ol>
<p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6b7lru822j20ei05ggmu.jpg"></p>
<p><img src="http://ww3.sinaimg.cn/mw690/63503acbjw1f6b7lryzw2j20ut0ntaok.jpg"></p>
<ol start="4">
<li>spring.factories中声明的类基本上都使用了@Conditional注解.</li>
</ol>
<h4 id="Conditional"><a href="#Conditional" class="headerlink" title="Conditional"></a>Conditional</h4><p>&nbsp;&nbsp;Spring Boot在org.springframework.boot.autoconfigure.condition包下定义了以下注解.</p>
<table>
<thead>
<tr>
<th>注解名</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>@ConditionalOnJava</td>
<td>基于JVM版本作为判断条件.</td>
</tr>
<tr>
<td>@ConditionalOnBean</td>
<td>当容器中有指定的Bean的条件下.</td>
</tr>
<tr>
<td>@ConditionalOnClass</td>
<td>当类路径下游指定的类的条件下.</td>
</tr>
<tr>
<td>@ConditionalOnExpression</td>
<td>基于SpEL表达式作为判断条件.</td>
</tr>
<tr>
<td>@ConditionalOnJndi</td>
<td>在JNDI存在的条件下查找指定的位置.</td>
</tr>
<tr>
<td>@ConditionalOnMissingBean</td>
<td>当容器中没有指定Bean的情况下.</td>
</tr>
<tr>
<td>@ConditionalOnMissingClass</td>
<td>当类路径下没有指定的类的情况下.</td>
</tr>
<tr>
<td>@ConditionalOnNotWebApplication</td>
<td>当前项目不是web项目的条件下.</td>
</tr>
<tr>
<td>@ConditionalOnProperty</td>
<td>指定的属性是否有指定的值.</td>
</tr>
<tr>
<td>@ConditionalOnResource</td>
<td>类路径是否有指定的值.</td>
</tr>
<tr>
<td>@ConditionalOnSingleCandidate</td>
<td>当指定Bean在容器中只有一个,或者虽然有多个但是指定首选的Bean.</td>
</tr>
<tr>
<td>@ConditionalOnWebApplication</td>
<td>当前项目是web项目的条件下.</td>
</tr>
</tbody></table>
<p>&nbsp;&nbsp;以上这些注解都组合了@Conditional元注解.</p>
<h3 id="分析-ConditionalOnNotWebApplication"><a href="#分析-ConditionalOnNotWebApplication" class="headerlink" title="分析@ConditionalOnNotWebApplication"></a>分析@ConditionalOnNotWebApplication</h3><p><img src="http://ww4.sinaimg.cn/mw690/63503acbjw1f6b8n61hytj20fb04dgmt.jpg"></p>
<p>&nbsp;&nbsp;@ConditionalOnNotWebApplication使用的条件类是OnWebApplicationCondition.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.springframework.boot.autoconfigure.condition;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionOutcome;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.condition.SpringBootCondition;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.ConditionContext;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.annotation.Order;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.type.AnnotatedTypeMetadata;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.ClassUtils;</span><br><span class="line"><span class="keyword">import</span> org.springframework.util.ObjectUtils;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.context.WebApplicationContext;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.context.support.StandardServletEnvironment;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Order(-2147483628)</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OnWebApplicationCondition</span> <span class="keyword">extends</span> <span class="title">SpringBootCondition</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String WEB_CONTEXT_CLASS = <span class="string">&quot;org.springframework.web.context.support.GenericWebApplicationContext&quot;</span>;</span><br><span class="line"></span><br><span class="line">    OnWebApplicationCondition() &#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ConditionOutcome <span class="title">getMatchOutcome</span><span class="params">(ConditionContext context, AnnotatedTypeMetadata metadata)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> webApplicationRequired = metadata.isAnnotated(ConditionalOnWebApplication.class.getName());</span><br><span class="line">        ConditionOutcome webApplication = <span class="keyword">this</span>.isWebApplication(context, metadata);</span><br><span class="line">        <span class="keyword">return</span> webApplicationRequired &amp;&amp; !webApplication.isMatch()?ConditionOutcome.noMatch(webApplication.getMessage()):(!webApplicationRequired &amp;&amp; webApplication.isMatch()?ConditionOutcome.noMatch(webApplication.getMessage()):ConditionOutcome.match(webApplication.getMessage()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> ConditionOutcome <span class="title">isWebApplication</span><span class="params">(ConditionContext context, AnnotatedTypeMetadata metadata)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(!ClassUtils.isPresent(<span class="string">&quot;org.springframework.web.context.support.GenericWebApplicationContext&quot;</span>, context.getClassLoader())) &#123;</span><br><span class="line">            <span class="keyword">return</span> ConditionOutcome.noMatch(<span class="string">&quot;web application classes not found&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(context.getBeanFactory() != <span class="keyword">null</span>) &#123;</span><br><span class="line">                String[] scopes = context.getBeanFactory().getRegisteredScopeNames();</span><br><span class="line">                <span class="keyword">if</span>(ObjectUtils.containsElement(scopes, <span class="string">&quot;session&quot;</span>)) &#123;</span><br><span class="line">                    <span class="keyword">return</span> ConditionOutcome.match(<span class="string">&quot;found web application \&#x27;session\&#x27; scope&quot;</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> context.getEnvironment() <span class="keyword">instanceof</span> StandardServletEnvironment?ConditionOutcome.match(<span class="string">&quot;found web application StandardServletEnvironment&quot;</span>):(context.getResourceLoader() <span class="keyword">instanceof</span> WebApplicationContext?ConditionOutcome.match(<span class="string">&quot;found web application WebApplicationContext&quot;</span>):ConditionOutcome.noMatch(<span class="string">&quot;not a web application&quot;</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>&nbsp;&nbsp;OnWebApplicationCondition在isWebApplication函数中进行条件判断.</p>
<ol>
<li>判断GenericWebApplicationContext是否在类路径中.</li>
<li>判断容器中是否存在名为session的scope.</li>
<li>判断当前容器的Environment是否为StandardServletEnvironment.</li>
<li>判断当前的ResourceLoader是否为WebApplicationContext.</li>
<li>最后通过ConditionOutcome.isMatch函数返回布尔值确定条件.</li>
</ol>
<h3 id="分析自动配置的实现"><a href="#分析自动配置的实现" class="headerlink" title="分析自动配置的实现"></a>分析自动配置的实现</h3><p>&nbsp;&nbsp;以http编码为例,如果在常规项目中则需要在web.xml中配置一个filter.而Spring Boot内置了http编码的自动配置,无需配置filter.</p>
<h4 id="properties配置类"><a href="#properties配置类" class="headerlink" title="properties配置类"></a>properties配置类</h4><p><img src="http://ww2.sinaimg.cn/mw690/63503acbjw1f6b9s692hxj20p30geq7e.jpg"></p>
<h4 id="自动配置Bean"><a href="#自动配置Bean" class="headerlink" title="自动配置Bean"></a>自动配置Bean</h4><p><img src="http://ww1.sinaimg.cn/mw690/63503acbjw1f6b9q6z1y8j20wc0idwkd.jpg"></p>
<p>&nbsp;&nbsp;@ConditionalOnProperty:当设置spring.http.encoding=enabled的情况下,如果没有设置则默认为true,即符合条件.</p>
<p>&nbsp;&nbsp;characterEncodingFilter()返回OrderedCharacterEncodingFilter这个对象,并根据注入的HttpEncodingProperties配置类设置参数.</p>
<p>&nbsp;&nbsp; @ConditionalOnMissingBean({CharacterEncodingFilter.class}):在容器中没有这个Bean的时候则新建这个Bean.</p>
<h3 id="end"><a href="#end" class="headerlink" title="end"></a>end</h3><blockquote>
<p>资料参考于 JavaEE开发的颠覆者: Spring Boot实战</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/Java/" rel="tag"># Java</a>
              <a href="/yuwanzi.io/tags/Spring/" rel="tag"># Spring</a>
              <a href="/yuwanzi.io/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/07/19/2016-07-19-Hadoop06-Storm/" rel="prev" title="Hadoop学习笔记(6)-Storm">
                  <i class="fa fa-chevron-left"></i> Hadoop学习笔记(6)-Storm
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2016/07/31/2016-07-31-Spring-data/" rel="next" title="Spring数据访问方案-Spring Data">
                  Spring数据访问方案-Spring Data <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
